package com.ruoyi.logistic.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkOutBoundSearchVo;
import com.ruoyi.logistic.domain.vo.QuotationVo;
import com.ruoyi.logistic.service.*;
import com.ruoyi.logistic.service.impl.NetworkOutboundSettlementServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/network/finance")
@RestController
@Api(tags = "网点财务_出港结算关系配置")
public class NetworkOutboundSettlementController {
    @Autowired
    private INetworkOutboundSettlementService networkOutboundSettlementService;
    @Autowired
    private IQuotationService quotationService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private FileService fileService;
    @Autowired
    private QuotationTypeService quotationTypeService;


    @ApiOperation("显示客户列表")
    @GetMapping("/selectAllClient")
    public R selectAllClient(){
        List<Client> clients = clientService.selectAll();
        if (clients == null || clients.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(clients);
        }
    }

    @ApiOperation("显示出港报价中心")
    @GetMapping("/selectQuotation")
    public R selectQuotation(PageDate pageDate){
        List<Quotation> quotationList = quotationService.selectAll(pageDate);
        if (quotationList == null || quotationList.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(PageInfo.info(quotationList));
        }
    }

    @ApiOperation("搜索出港报价中心")
    @PostMapping("/searchQuotation")
    @ResponseBody
    public R searchQuotation(QuotationVo quotationVo){
        if (quotationVo.getStatus() == 1){
            return R.ok(PageInfo.info(quotationService.selectAll(quotationVo.getPageDate())));
        }else {
            List<Quotation> quotationList = quotationService.search(quotationVo);
            if (quotationList == null || quotationList.size() == 0){
                return R.fail("暂无数据");
            }else {
                return R.ok(PageInfo.info(quotationList));
            }
        }
    }

    @ApiOperation("显示报价类型列表")
    @GetMapping("/selectQuotationAll")
    public R selectQuotationAll(){
        List<QuotationType> quotationTypeList = quotationTypeService.selectAll();
        if (quotationTypeList == null || quotationTypeList.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(quotationTypeList);
        }
    }

    @ApiOperation("显示出港结算关系配置列表")
    @GetMapping("/selectNetworkOutboundSettlementAll")
    public R selectNetworkOutboundSettlementAll(PageDate pageDate){
        List<NetworkOutboundSettlement> networkOutboundSettlements = networkOutboundSettlementService.selectAll(pageDate);
        if (networkOutboundSettlements == null || networkOutboundSettlements.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(PageInfo.info(networkOutboundSettlements));
        }
    }
    @ApiOperation("添加出港结算关系配置")
    @PostMapping("/insertNetworkOutboundSettlement")
    public R insertNetworkOutboundSettlement(@RequestBody NetworkOutboundSettlement networkOutboundSettlement){
        Boolean isAdd = networkOutboundSettlementService.insert(networkOutboundSettlement);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @ApiOperation("修改出港结算关系配置")
    @PostMapping("/updateNetworkOutboundSettlement")
    public R updateNetworkOutboundSettlement(@RequestBody NetworkOutboundSettlement networkOutboundSettlement){
        Boolean isUpdate = networkOutboundSettlementService.update(networkOutboundSettlement);
        if (isUpdate){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @ApiOperation("删除出港结算关系配置")
    @PostMapping("/deleteNetworkOutboundSettlement")
    public R deleteNetworkOutboundSettlement(Integer id){
        Boolean isDelete = networkOutboundSettlementService.deleteById(id);
        if (isDelete){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @ApiOperation("添加客户")
    @PostMapping("/insertClient")
    public R insertClient(@RequestBody Client client){
        Boolean isAdd = clientService.insert(client);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }

    @ApiOperation("添加报价")
    @PostMapping("/insertQuotation")
    public R insertQuotation(@RequestBody OutboundQuotation outboundQuotation){
        Boolean isAdd = networkOutboundSettlementService.insertMessage(outboundQuotation);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
//    @ApiOperation("批量关联报价")
//    @GetMapping("/selectQuotation")
//    public R selectQuotation(PageDate pageDate){
//        List<Quotation> quotations = quotationService.selectAll(pageDate);
//        if (quotations == null || quotations.size() == 0){
//            return R.fail("暂无数据");
//        }else {
//            return R.ok(PageInfo.info(quotations));
//        }
//    }
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteByIds")
    public R deleteNetworkOutboundSettlementByIds(@RequestParam("ids") List<Integer> ids){
        Integer count = networkOutboundSettlementService.deleteByIds(ids);
        if (count > 0){
            return R.ok("批量删除成功");
        }else {
            return R.fail("批量删除失败");
        }
    }
    @ApiOperation("批量导出")
    @GetMapping("/uploadNetworkOutboundSettlement")
    public ResponseEntity<byte[]> uploadNetworkOutboundSettlement(@RequestParam("ids") Integer [] ids,HttpServletResponse response) throws IOException{
        return fileService.downloadNetworkOutboundSettlement(ids,response);
    }
    @ApiOperation("查询出港结算关系配置")
    @PostMapping("/search")
    @ResponseBody
    public R search(@RequestBody NetworkOutBoundSearchVo networkOutboundSettlementVo){
        List<NetworkOutboundSettlement> outboundSettlementList = networkOutboundSettlementService.search(networkOutboundSettlementVo);
        if (outboundSettlementList == null || outboundSettlementList.size() == 0) {
            return R.fail("暂无数据");
        }else {
            return R.ok(PageInfo.info(outboundSettlementList));
        }
    }


}
